﻿using System;

namespace FlightAnalyzer {

	public class Weather {
		public string description { get; set; }
		public string location { get; set; }
		public string lastUpdated { get; set; }
		public string temperature { get; set; }
		public float temp_celsius { get; set; }
		public string windSpeed { get; set; }
		public float windSpeed_mph { get; set; }
		public float windGust_mph { get; set; }
		public string precipitation { get; set; }
		public float rain { get; set; }
		public int weatherAffect { get; set; }

		public Weather (string newDesc, string newLocation, string newLastUpdated, string newTemperature,  
			string newWindSpeed, string newPrecipitation) {

			description = newDesc;
			location = newLocation;
			lastUpdated = newLastUpdated;
			temperature = newTemperature;
			windSpeed = newWindSpeed;
			precipitation = newPrecipitation;

			temp_celsius = parseTemperature ();
			windSpeed_mph = parseWindSpeed ();
			windGust_mph = parseWindGust ();
			rain = parsePrecipitation ();

			weatherAffect = computeWeatherAffect ();

		}

		public float parseTemperature() {
			// Parse the string associated with temperature.
			string[] temp = new string[] {"0", "C"};
			float temp_c = 0;
			string[] lines = temperature.Split ('(', ')');
			foreach (string line in lines) {
				if (line.IndexOf ("C") != -1) {
					temp = line.Split ();
				}
			}

			try {
				temp_c = float.Parse(temp[0]);
			}
			catch (Exception e) {
				if (e is FormatException) {
					Console.WriteLine("temp is not a sequence of digits.");
				}
				if (e is OverflowException) {
					Console.WriteLine ("The number cannot fit in an Int32.");
				}
			}
			finally {
				Console.WriteLine("The converted value is {0}", temp_c);
			}

			return temp_c;
		}

		public float parseWindSpeed() {
			// Parse the string associated with the wind speed.
			float lowspeed = 0;
			float highspeed = 0;
			float tempF = 0;
			string[] lines = windSpeed.Split ();
			foreach (string line in lines) {
				if (line.IndexOf("calm") != -1) {
					windSpeed_mph = 0;
					return 0;
				}
				if (float.TryParse(line, out tempF)) {
					if (lowspeed == 0) {
						lowspeed = tempF;
						Console.WriteLine ("Lowspeed is: {0}", lowspeed);
					}
					else {
						highspeed = tempF;
						Console.WriteLine ("High speed is: {0}", highspeed);
					}
				}
			}
			return lowspeed;
		}

		public float parseWindGust() {
			// Parse the string associated with wind gusts.
			float lowspeed = 0;
			float highspeed = 0;
			float tempF = 0;
			string[] lines = windSpeed.Split ();
			foreach (string line in lines) {
				if (line.IndexOf("calm") != -1) {
					windSpeed_mph = 0;
					return 0;
				}
				if (float.TryParse(line, out tempF)) {
					if (lowspeed == 0) {
						lowspeed = tempF;
					}
					else {
						highspeed = tempF;
					}
				}
			}
			return highspeed;
		}

		public float parsePrecipitation() {
			// Parse the string associated with precipitation.
			string[] rain = new string[] {"0", "mm"};
			float rain_mm = 0;
			string[] lines = precipitation.Split ('(', ')');
			foreach (string line in lines) {
				Console.WriteLine (line);
				if (line.IndexOf ("mm") != -1) {
					rain = line.Split ();
				}
			}

			try {
				rain_mm = float.Parse(rain[0]);
			}
			catch (Exception e) {
				if (e is FormatException) {
					Console.WriteLine("string is not a sequence of digits.");
				}
				if (e is OverflowException) {
					Console.WriteLine ("The number cannot fit in an Int32.");
				}
			}
			finally {
				Console.WriteLine("The converted value is {0}", rain_mm);
			}

			return rain_mm;
		}

		public int computeWeatherAffect() {
			// Our three areas of interest that primarily affect flight schedules are:
			// - Temperature
			// - Precipitation
			// - Wind Speed and Wind Gusts.
			// Precipitation has the greatest affect, followed by temperature, followed by wind speed.
			// Precipitation and temperature must be considered together, since freezing rain or snow amplifies the affects of
			// precipitation.
			int weatherFactor = 0;

			// Calculate precipitation affect.
			if (rain < 15) {
				if (temp_celsius <= 0) {
					Console.WriteLine ("temperature is less than 0.");
					weatherFactor = weatherFactor + 1;
					Console.WriteLine ("weatherFactor is: {0}", weatherFactor);
				}
			} else if (rain < 25) {
				if (temp_celsius <= 0) {
					weatherFactor = weatherFactor + 2;
				} else {
					weatherFactor = weatherFactor + 1;
				}
			} else if (rain < 35) {
				if (temp_celsius <= 0) {
					weatherFactor = weatherFactor + 3;
				} else {
					weatherFactor = weatherFactor + 2;
				}
			} else if (rain < 45) {
				if (temp_celsius <= 0) {
					weatherFactor = weatherFactor + 4;
				} else {
					weatherFactor = weatherFactor + 3;
				}
			} else if (rain < 55) {
				if (temp_celsius <= 0) {
					weatherFactor = weatherFactor + 5;
				} else {
					weatherFactor = weatherFactor + 4;
				}
			} else if (rain < 65) {
				if (temp_celsius <= 0) {
					weatherFactor = weatherFactor + 6;
				} else {
					weatherFactor = weatherFactor + 5;
				}
			} else if (rain >= 65) {
				if (temp_celsius <= 0) {
					weatherFactor = weatherFactor + 7;
				} else {
					weatherFactor = weatherFactor + 6;
				}
			}

			if (windSpeed_mph >= 60) {
				weatherFactor = weatherFactor + 6;
			} else if (windSpeed_mph >= 50) {
				weatherFactor = weatherFactor + 5;
			} else if (windSpeed_mph >= 40) {
				weatherFactor = weatherFactor + 4;
			} else if (windSpeed_mph >= 35) {
				weatherFactor = weatherFactor + 3;
			} else if (windSpeed_mph >= 30) {
				weatherFactor = weatherFactor + 2;
			} else if (windSpeed_mph >= 25) {
				weatherFactor = weatherFactor + 1;
			}

			if (windGust_mph > (windSpeed_mph + 10)) {
				weatherFactor = weatherFactor + 1;
			}

			return weatherFactor;
		}
	}
}


